<?php
/**
 * 公共信息控制器，登录，验证登录
 * 
 * @author Lee<a605333742@gmail.com>
 * @date    2018-06-26
 */
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Admin;
use app\admin\model\Role;
use app\admin\model\Log;
use think\Request;
use think\Session;

class Login extends Controller{
    protected $adminModel;
    protected $roleModel;
    protected $logModel;
    public function _initialize() {
        parent::_initialize();
        $this->adminModel   =new Admin();
        $this->roleModel    =new Role();
        $this->logModel     =new Log();
    }
    
    /*
     * 登录页面
     * 
     * @return #
     */
    public function index(){        
        $is_captcha=0;
        if(config('is_captcha')==TRUE){
            $is_captcha=1;
        }
        $this->assign('is_captcha',$is_captcha);
        return $this->fetch();
    }
    
    /*
     * 验证登录
     * 
     * @param str $username 用户名
     * @param str $password 密码
     * 
     * @return #
     */
    public function checkLogin(){       
        $data   =$this->request->param();
		
        $request= Request::instance();
        $map['name|tel|email']= trim($data['username']); 
        $filed='id,name,nick_name,password,last_logintime,last_ip,inc,status';
        $userInfo=$this->adminModel->getOneInfo($map,$filed);
        //检验验证码		
        if(isset($data['captcha']) && !empty($data['captcha'])&& isset($data['is_ajax'])&&$data['is_ajax']==1){
            if(!captcha_check($data['captcha'])){
                ret_msg(10001);
            }
        }
        if(!$userInfo){
            ret_msg(10002);
        }
        if(md5(trim($data['password']))!=$userInfo['password']){
            ret_msg(10002);
        }
        if($userInfo['status']==2){
            ret_msg(10003);
        }  
        //权限验证（admin用户不需要验证权限）
        if(trim($userInfo['name'])!='admin'&& self::checkRole($userInfo['id']==10004)){
            ret_msg(10004);
        }
        //添加登录日志
        $logCondition=array(
            'a_id'      =>$userInfo['id'],
            'content'   =>'用户 '.$userInfo['name'].' 登陆成功',
            'add_time'  =>time()
        );
        $this->logModel->addOneLog($logCondition);
        unset($userInfo['password']);
        $condition['id']=$userInfo['id'];
        $map=array(
            'inc'       =>$userInfo['inc']+1,
            'last_ip'   =>$request->ip(),
            'last_logintime'=>time()
        );
        $this->adminModel->saveOneData($condition,$map);
        $userInfo['inc']=$map['inc'];
        session('is_login',1);
        session('user',$userInfo);   
        $this->redirect('Index/index');
    }
    
    /*
     * 创建验证码
     * 
     * @return #
     */
    public function show_captcha(){
        $captcha = new \think\captcha\Captcha();
        $captcha->imageW    =131;
        $captcha->imageH    = 32;  //图片高
        $captcha->fontSize  =14;  //字体大小
        $captcha->length    = 4;  //字符数
        $captcha->fontttf   = '5.ttf';  //字体
        $captcha->expire    = 30;  //有效期
        $captcha->useNoise  = false;  //不添加杂点
        return $captcha->entry();
    }
    
    /*
     * 检查用户角色，并且根据角色查询出所拥有的节点列表
     * 
     * @param int $a_id 用户ID
     * @return array $node_list
     */
    public function checkRole($a_id='',$role_id=''){
        if(empty($role_id)){
            $role_id=$this->roleModel->getOneRoleId(array('a_id'=>$a_id),'role_id');
        }
        $roleInfo   = $this->roleModel->getOneRoleInfo(array('id'=>$role_id,'status'=>1));
        if(!$roleInfo){
            return 10004;
        }
        session('roleInfo',$roleInfo);
        return true;
    }
    
    /*
     * 退出
     * 
     * @return #
     */
    public function logout(){
        Session::clear();
        $this->redirect('index');
    }
    
    /*
     * ajax验证用户名密码是否正确
     * 
     * @param array $data 用户名，密码，验证码
     * @return json 错误码，错误信息，数据
     */
    public function ajaxCheck(){
        $data   =$this->request->param();
        $map['name|tel|email']= trim($data['username']); 
        $filed='id,name,nick_name,password,last_logintime,last_ip,inc,status';
        $userInfo=$this->adminModel->getOneInfo($map,$filed);
        //检验验证码		
        if(isset($data['captcha']) && !empty($data['captcha'])&& isset($data['is_ajax'])&&$data['is_ajax']==1){
            if(!captcha_check($data['captcha'])){
                ret_msg(10001);
            }
        }
        if(!$userInfo){
            ret_msg(10002);
        }
        if(md5(trim($data['password']))!=$userInfo['password']){
            ret_msg(10002);
        }
        if($userInfo['status']==2){
            ret_msg(10003);
        }  
        ret_msg(0);
    }
}
